package com.tanhua.server.service;

import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 圈子互动业务处理层
 */
@Service
@Slf4j
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     *   评论分页列表
     *   movementId:动态表主键id
     */
    public PageResult<CommentVo> findPageComment(Long page, Long pagesize, String movementId) {
        log.debug("*******page{}****pagesize{}****movementId{}**********",page,pagesize,movementId);
        //1.根据动态id分页查询评论列表数据
        PageResult<Comment> pageResult = commentApi.findPageComment(page,pagesize,movementId);
        log.debug("*******评论分页列表{}*********",pageResult);
        if(pageResult == null || CollectionUtils.isEmpty(pageResult.getItems())){
            return new PageResult<>(1l,10l,0l,null);//前端问题
        }
        //2.获取所有评论者ids(comment表中userId)
        List<Comment> commentList = pageResult.getItems();
        List<Long> commentUserIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());
        //3.根据评论者ids查询用户详情
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByCondition(commentUserIds,null);
        //4.基于用户详情和评论数据构造vo返回
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoMap.get(comment.getUserId());//评论人用户信息
            commentVoList.add(CommentVo.init(userInfo,comment));
        }
        log.debug("*******commentVoList{}*********",commentVoList);
        return new PageResult<>(page, pagesize, pageResult.getCounts(), commentVoList);
    }

    /**
     * 评论-提交(动态评论)
     */
    public void addComment(String publishId, String content) {
        log.debug("*******publishId{}*****content{}****",publishId,content);
        //1.构造Comment对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));//动态id
        comment.setCommentType(CommentType.COMMENT.getType());//评论类型，1-点赞，2-评论，3-喜欢
        comment.setContent(content);//动态评论内容
        comment.setUserId(UserHolder.getUserId());//评论人用户id
        //comment.setPublishUserId();//被评论人的用户id(提供者那边实现)
        //2.调用服务保存动态评论记录
        Integer count = commentApi.add(comment);
        log.debug("********动态评论数量******count{}*************",count);
    }
}
